package com.example.module.dialog;


import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.text.Editable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.R;
import com.example.entity.Member;
import com.example.module.api.APIRequest;
import com.example.module.api.AsyncHttpTask;
import com.example.module.api.HttpGetTask;
import com.example.module.api.HttpMethod;
import com.example.module.api.HttpTaskReponse;
import com.example.module.api.ResponseCode;
import com.example.util.StringUtil;
import com.google.gson.Gson;

public class LoginLayout extends LinearLayout {
	private static final String TAG = "LoginLayout";
	
	private boolean isPasswordOnly;
	private Button loginButton;
	private EditText emailEditText, passwordEditText;
	private TextView emailTextView, passwordTextView;
	
	private TextView passcodeText, resultText;
	
	private View passcodeArea;
	
	private LoginListener loginListener;
	
	public interface LoginListener {
		public void OnSuccess();
		public void OnFail();
	}

	public LoginLayout(Context context) {
		super(context);
		initializeView(context);
		initializeAction(context);
	}
	
	public LoginLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		initializeView(context);
		initializeAction(context);
	}
	
	public static String getString(Editable e) {
		if (e == null)
			return "";
		
		return StringUtil.nvl(e.toString());
	}

	private void initializeView(Context context) {
		View layout = inflate(context, R.layout.form_login, this);
		emailTextView = (TextView) layout.findViewById(R.id.emailTextView);
		emailEditText = (EditText) layout.findViewById(R.id.emailEditText);
		passwordTextView = (TextView) layout.findViewById(R.id.passwordTextView);
		passwordEditText = (EditText) layout.findViewById(R.id.passwordEditText);
		
		resultText = (TextView) layout.findViewById(R.id.resultText);
		
        loginButton =  (Button) findViewById(R.id.loginButton);
        
        isPasswordOnly = this.getTag() != null && "PasscodeOnly".equals(this.getTag().toString()); 
		if (isPasswordOnly) {
			emailTextView.setVisibility(View.GONE);
			emailEditText.setVisibility(View.GONE);
		}
		else {
			emailTextView.setVisibility(View.VISIBLE);
			emailEditText.setVisibility(View.VISIBLE);
		}
	}

	private void initializeAction(Context context) {
		loginButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				HttpGetTask task = new HttpGetTask(getContext());
				task.setmHttpReponseListener(new AsyncHttpTask.HttpReponseListener() {

					@Override
					public void OnResponse(HttpTaskReponse response) {
						Log.d(TAG, "response=" + response);
						
						if (response.getCode() == AsyncHttpTask.RESPONSE_OK) {
							try {
								JSONObject json = new JSONObject(response.getMessage());
								
								Log.d(TAG, "ResponseCode.OK=" + ResponseCode.OK);
								
								if (json.has("code") && ResponseCode.OK.equals(json.getString("code"))) {
									JSONArray members = json.getJSONArray("member");
									
									for (int i = 0; members!= null && members.length() > 0; i++) {
										
										Member member = new Gson().fromJson(members.get(i).toString(), Member.class);
										Log.d(TAG, "_id=" + member.get_id());
										Log.d(TAG, "Email=" + member.getEmail());
										Log.d(TAG, "Level=" + member.getLevel());
										Log.d(TAG, "CreateDate=" + member.getCreateDate());
									}
									
									Member member = new Gson().fromJson(json.getString("member"), Member.class);
									Log.d(TAG, "_id=" + member.get_id());
									Log.d(TAG, "Email=" + member.getEmail());
									Log.d(TAG, "Level=" + member.getLevel());
									Log.d(TAG, "CreateDate=" + member.getCreateDate());
//									Card card = new Gson().fromJson(((JSONObject) cardArray.get(i)).toString(), Card.class);
								}	
							}
							catch (Exception e) {
								e.printStackTrace();
							}
						}
						
						if (loginListener != null) {
							loginListener.OnSuccess();
						}
					}
				});
				
				
				String url = "http://54.68.14.161/api/member";
				String email = getString(emailEditText.getText());
				String password = getString(passwordEditText.getText());
				HashMap<String, String> param = new HashMap<String, String>();
				param.put("email", email);
				param.put("password", password);
				
				try {
					task.execute(new APIRequest(url, HttpMethod.GET, param));
				}
				catch (Exception e) {
					e.printStackTrace();
				}
				
				
			}
		});
	}

	public void setEmail(String email) {
		emailEditText.setText(email);
		emailEditText.setEnabled(false);
	}
	
	public void setPassword(String password) {
		passwordEditText.setText(password);
//		passwordEditText.setEnabled(false);
	}

	public View getPasscodeArea() {
		return passcodeArea;
	}

	public void setPasscodeArea(View passcodeArea) {
		this.passcodeArea = passcodeArea;
	}
	
	public LoginListener getLoginListener() {
		return loginListener;
	}

	public void setLoginListener(LoginListener loginListener) {
		this.loginListener = loginListener;
	}
}
